
local unlockCfg = {
	coin = 300,
	-- rmb = 2,
}

---------------
UnlockPanel = class("UnlockPanel")

function UnlockPanel.create(parent)
    local panel = UnlockPanel.new()
    panel:init(parent)

    panel.view:setPosition(pp.center.x, pp.center.y)
    uiEffect.bounceIn(panel.view)    
    parent:base_onOpenWin(panel)

    return panel
end


function UnlockPanel:init(parent)
    self.view = ccs.GUIReader:getInstance():widgetFromJsonFile("unlockPanel.json")
    parent.widget:addChild(self.view)
    self.view:setLocalZOrder(pp.z_pause)
    self.parent = parent

    local function btCloseCallback(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            AudioEngine.playEffect(sound.btn2)
        elseif eventType == ccui.TouchEventType.ended then
            uiEffect.bounceOut(self.view, true)
            self.parent:base_onCloseWin(self)
            
        end
    end
    self.bt_close = self.view:getChildByName("_close")
    self.bt_close:addTouchEventListener(btCloseCallback)

    --
    -- self.coin_desc = uiUtil.createBMFontByHolder(self.view, "_coin_desc", pp.fnt_unlock)
    -- self.coin_desc:setString(string.format("搜集%d金币,免费解锁", unlockCfg.coin))
    -- self.coin_desc:setScale(0.7)
    
    --bt1
    local function btCoinCallback(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            AudioEngine.playEffect(sound.btn1)
         elseif eventType == ccui.TouchEventType.ended then
            if userData.coin >= unlockCfg.coin then
                userData.coin = userData.coin - unlockCfg.coin
                parent:refreshCoin(userData.coin)	

                parent:unlockLock()

             	uiEffect.bounceOut(self.view, true)
                self.parent:base_onCloseWin(self)
            else
                uiPopNotify:popString(self.parent.widget, "金币不足")
                if DEF_CHN_XNY then
                    pchCenter:order(pchCfg.item.JinBiLiBao)
                else
                    WarningPanel.create(self.parent, "Pay_level_Libao_Jinbi")
                end
            end
           
         end
    end
    self.bt_coin = self.view:getChildByName("_bt_coin")
    self.bt_coin:addTouchEventListener(btCoinCallback)
    local fntcoin = uiUtil.createBMFontByHolder(self.view, "_fnt_coin", pp.fnt_number_yellow)
    fntcoin:setString(string.format("%d", unlockCfg.coin))
    fntcoin:setScale(0.7)
    
    -- --bt2
    -- local function btRMBCallback(sender, eventType)
    --     if eventType == ccui.TouchEventType.ended then
    --         -- parent:unlockLock()

    --         -- uiEffect.maskOut(self.parent.mask)
    --         -- uiEffect.bounceOut(self.view, true)
    --         -- self.parent.unlockPanel = nil
    --     end
    -- end
    -- self.bt_rmb = self.view:getChildByName("_bt_rmb")
    -- self.bt_rmb:addTouchEventListener(btRMBCallback)
    -- local fntrmb = uiUtil.createBMFontByHolder(self.bt_rmb, "_fnt_rmb", pp.fnt_unlock)
    -- fntrmb:setString(string.format("%d元", unlockCfg.rmb))
end 